home *** CD-ROM | disk | FTP | other *** search
- /* © 1988, Bowers Development Corp. */
- /* EventLoop.c */
-
- #include "Globals.h"
- #include "Cursors.h"
- #include "DoScrap.h"
- #include "Miscellany.h"
- #include "WindowAids.h"
- #include "Windowing.h"
- #include "Dispatcher.h"
- #include "MainMenu.h"
-
- #include "EventLoop.h"
-
- Boolean hasWNE;
-
- /*----------*/
- void DoMouseDown (void);
- void DoMouseUp (void);
- void DoKeystroke (void);
- void DoUpdate (void);
- void DoActivate (void);
- void DoDiskEvent (void);
- void DoApp4Event (void);
-
- #define _WaitNextEvent 0xA860
-
- /*----------*/
- Boolean TrapAvailable (short tNumber,
- TrapType tType);
- Boolean TrapAvailable (tNumber, tType)
- short tNumber;
- TrapType tType;
- {
- return (false);
-
- /*----- not yet supported by THINK -----
- return (NGetTrapAddress (tNumber, tType)
- != GetTrapAddress (_Unimplemented));
- -----*/
- } /*TrapAvailable*/
-
- /*----------*/
- void SetStackSize (long stackSize);
- void SetStackSize (stackSize)
- long stackSize;
- {
- SetApplLimit ((Ptr) ((long) &stackSize - stackSize));
- } /*SetStackSize*/
-
- /*----------*/
- void Initialize ()
- {
- #define sysEnvironsVersion 1
-
- SysEnvRec environs;
- OSErr ignoreError;
-
- SetStackSize (15000);
- MaxApplZone ();
- MoreMasters ();
-
- InitGraf (&thePort);
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (nil);
-
- FlushEvents (everyEvent, 0);
-
- ignoreError = SysEnvirons (sysEnvironsVersion, &environs);
- if (environs.machineType < 0) {
- hasWNE = false;
- } else {
- hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
- }
-
- inBackground = false;
-
- LoadMenus ();
- LoadCursors ();
- InitScrap ();
-
- InitGlobals ();
-
- InitModelessDialogs ();
- InitTitles (); /*initialize each menu title's module*/
- } /*Initialize*/
-
- /*----------*/
- void DoMouseDown ()
- {
- WindowPtr whichWindow;
- short whichPart;
-
- whichPart = FindWindow (curEvent.where, &whichWindow);
- switch (whichPart) {
- case inDesk:
- /*Do nothing*/;
- break;
- case inMenuBar:
- DoMenu (MenuSelect (curEvent.where));
- break;
- case inSysWindow:
- SystemClick (&curEvent, whichWindow);
- break;
- case inContent:
- DoContent (whichWindow);
- break;
- case inDrag:
- DoDrag (whichWindow);
- break;
- case inGrow:
- DoGrow (whichWindow);
- break;
- case inGoAway:
- DoGoAway (whichWindow);
- break;
- case inZoomIn:
- DoZoom (whichWindow, inZoomIn);
- break;
- case inZoomOut:
- DoZoom (whichWindow, inZoomOut);
- break;
- } /*switch*/
- } /*DoMouseDown*/
-
- /*----------*/
- void DoMouseUp ()
- {
- WindowPtr whichWindow;
- short whichPart;
-
- whichPart = FindWindow (curEvent.where, &whichWindow);
- /*save event time to check later for double click*/
- } /*DoMouseUp*/
-
- /*----------*/
- void DoKeystroke ()
- {
- short charCode;
- char ch;
-
- charCode = curEvent.message & charCodeMask;
- ch = charCode;
-
- if ((curEvent.modifiers & cmdKey) != 0) {
- if (curEvent.what != autoKey) {
- DoMenu (MenuKey (ch));
- }
- } else {
- if (curWindow == nil) {
- SysBeep (1);
- } else {
- TypeInWindow (ch);
- }
- }
- } /*DoKeystroke*/
-
- /*----------*/
- void DoUpdate ()
- {
- GrafPtr savePort;
- WindowPtr saveWindow;
- WindowPtr whichWindow;
-
- GetPort (&savePort);
- whichWindow = (WindowPtr) curEvent.message;
- SetPort (whichWindow);
- saveWindow = curWindow;
- SetInfo (whichWindow);
- BeginUpdate (whichWindow);
- EraseRect (&(whichWindow->portRect));
- DrawControls (whichWindow);
- UpdateContent ();
- EndUpdate (whichWindow);
- SetInfo (saveWindow);
- SetPort (savePort);
- } /*DoUpdate*/
-
- /*----------*/
- void DoActivate ()
- {
- Boolean activate;
- WindowPtr whichWindow;
-
- whichWindow = (WindowPtr) curEvent.message;
- SetPort (whichWindow);
- SetInfo (whichWindow);
-
- activate = ((curEvent.modifiers & activeFlag) != 0);
- if (activate) {
- if ((cur->text) != nil) {
- TEActivate (cur->text);
- }
- ReadDeskScrap ();
- }
-
- HiliteScroll (cur->vScroll, activate);
- HiliteScroll (cur->hScroll, activate);
-
- ActivateContent (activate);
-
- if (!activate) {
- WriteDeskScrap ();
- if (cur->text != nil) {
- TEDeactivate (cur->text);
- }
- SetInfo (nil);
- }
- } /*DoActivate*/
-
- /*----------*/
- void DoDiskEvent ()
- {
- #define dlgTop 75
- #define dlgLeft 100
-
- OSErr ignore;
- Point where;
-
- if (HiWord (curEvent.message) != noErr) {
- DILoad ();
- SetPt (&where, dlgLeft, dlgTop);
- ignore = DIBadMount (where, curEvent.message);
- DIUnload ();
- }
- } /*DoDiskEvent*/
-
- /*----------*/
- void DoApp4Event ()
- {
- #define suspendResumeMsg 1
- #define resumeMask 1
-
- Boolean resume;
- WindowPtr front;
-
- curEvent.what = nullEvent;
- if (BitShift (curEvent.message, -24) == suspendResumeMsg) {
- resume = ((curEvent.message & resumeMask) != 0);
- if (resume) {
- ReadDeskScrap ();
- } else {
- WriteDeskScrap ();
- }
- inBackground = !resume;
- front = FrontWindow ();
- if (front != nil) {
- curEvent.what = activateEvt;
- curEvent.modifiers = LoWord (curEvent.message);
- curEvent.message = (long) front;
- }
- }
- } /*DoApp4Event*/
-
- /*----------*/
- long GetSleep (void);
- long GetSleep ()
- {
- long sleep;
-
- sleep = 10000;
- if ((!inBackground) && (FrontWindow () != nil)) {
- if (FrontWindow () == curWindow) {
- if (cur->text != nil) {
- if ((**(cur->text)).selStart == (**(cur->text)).selEnd) {
- sleep = GetCaretTime ();
- }
- }
- } else { /* DA or modeless dialog */
- sleep = GetCaretTime ();
- }
- }
- return (sleep);
- } /*GetSleep*/
-
- /*----------*/
- Boolean GetEvent (void);
- Boolean GetEvent ()
- {
- Boolean gotEvent;
-
- if (hasWNE) {
- gotEvent = WaitNextEvent (everyEvent, &curEvent,
- GetSleep (), cursorRgn);
- } else {
- SystemTask ();
- gotEvent = GetNextEvent (everyEvent, &curEvent);
- }
- return (gotEvent);
- } /*GetEvent*/
-
- /*----------*/
- Boolean DoEvent (void);
- Boolean DoEvent ()
- {
- Boolean gotEvent;
- DialogPtr whichDialog;
- short itemHit;
-
- gotEvent = GetEvent ();
- if (curEvent.what == app4Evt) {
- /* here so event won't get swallowed by IsDialogEvent */
- /* and so suspend/resume can be translated to activate */
- DoApp4Event ();
- }
- if (IsDialogEvent (&curEvent)) {
- if ((curEvent.what == activateEvt)
- || (curEvent.what == updateEvt)) {
- whichDialog = (DialogPtr) curEvent.message;
- } else {
- whichDialog = FrontWindow ();
- }
- SetPort (whichDialog);
- if ((Boolean) FilterModeless (whichDialog, &curEvent, &itemHit)) {
- DoModelessItem (whichDialog, itemHit);
- } else if (DialogSelect (&curEvent, &whichDialog, &itemHit)) {
- DoModelessItem (whichDialog, itemHit);
- }
- } else if (gotEvent) {
- switch (curEvent.what) {
- case mouseDown:
- DoMouseDown ();
- break;
- case mouseUp:
- DoMouseUp ();
- break;
- case keyDown:
- case autoKey:
- DoKeystroke ();
- break;
- case updateEvt:
- DoUpdate ();
- break;
- case activateEvt:
- DoActivate ();
- break;
- case diskEvt:
- DoDiskEvent ();
- break;
- } /*switch*/
- }
- return (gotEvent);
- } /*DoEvent*/
-
- /*----------*/
- void MainLoop ()
- {
- UpdateMenus ();
-
- quittingTime = false;
- while (!quittingTime) {
- ShapeCursor ();
- if (cur->text != nil) {
- TEIdle (cur->text);
- }
- if (DoEvent ()) {
- UpdateMenus ();
- }
- } /*while*/
-
- WriteDeskScrap ();
- } /*MainLoop*/
-